<?php
namespace Home\Model;

use Think\Model;

class UserModel extends Model{

    const user = null;
    const connection = null;
    /**
     * 公共构造方法
     */
    public function __construct()
    {
        parent::__construct();
        $this->user = M("user");
        $this->Model = new \Think\Model();
        $this->connection = $this->db(1,"mysql://root:123456@localhost:3306/mmy2");
    }


    /**
     *  function select
     * @return mixed
     */
    public function getUserList()
    {
        $data =  $this->user->where('status = 1')->select();
        return $data;
    }

    /**
     * function insert
     * @param $data
     * @return bool
     */
    public function InsertUser($data)
    {
        if(!count($data))
        {
            return false;
        }

        $res = $this->user->data($data)->add();
        return $res;

    }

    /**
     * function mode
     * @param $data
     * @return bool
     */
    public function ModUser($data)
    {
        if(!count($data))
        {
            return false;
        }

        $res = $this->user->save($data);
        return $res;
    }

    /**
     * function delete
     * @param $UserId
     * @return bool
     */
    public function DeleteUser($UserId)
    {
        if(empty($UserId))
        {
            return false;
        }

        $res = $this->user->where('id='.$UserId)->delete();
        return $res;

    }

    /**
     * 切换数据库获取数据
     */
    public function getUserList2()
    {
        $sql = "select id,name,status from mmy2_person";
        $res =$this->connection->query($sql);
        return $res;
    }

    /**
     * 判断用户名是否已经存在
     * @author mmy
     * @param $name
     * @return mixed
     */
    public function checkIfUserExist($name)
    {
        if(empty($name))
        {
            return false;
        }
        $string = "username ='".$name."'";
        $data =  $this->user->where($string)->select();
        return $data;
    }

    /**
     * 登录方法
     * @author mmy
     * @param $name
     * @param $psw
     */
    public function login($name,$psw)
    {
        if(empty($name)||empty($psw))
        {
            return false;
        }

        $string = "username ='".$name."' and passwd='".$psw."'";
        $data =  $this->user->where($string)->select();
        return $data;
    }

    /**
     * 用户登录日志
     * @author mmy
     * @param $username
     */
    public function loginLog($userId)
    {
        if(empty($userId))
        {
            return false;
        }
        $time = date('Y-m-d H:i:s',time());
        $sql = "insert into book_user_loginlog (userid,intime) values ('".$userId."','".$time."')";
        $res = $this->Model->execute($sql);
        return $res;
    }

}


